package com.bestcem.xm.award.grpc.service;

import com.bestcem.bp.grpc.v1.base.Code;
import com.bestcem.xm.award.dao.AwardAwardDao;
import com.bestcem.xm.award.entity.pojo.AwardAwardDO;
import com.bestcem.xm.award.enums.AwardStatusEnum;
import com.bestcem.xm.award.enums.AwardTypeEnum;
import com.bestcem.xm.award.grpc.client.ProjectGrpcService;
import com.bestcem.xm.award.grpc.v1.services.AwardMqInitServiceGrpc;
import com.bestcem.xm.award.grpc.v1.services.InitAwardMqRequest;
import com.bestcem.xm.award.grpc.v1.services.InitAwardMqResponse;
import com.bestcem.xm.award.service.impl.DrawAwardServiceImpl;
import com.bestcem.xm.award.service.impl.RedPacketAwardServiceImpl;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.qdes.grpc.v1.services.GetProjectTitleRequest;
import com.bestcem.xm.qdes.grpc.v1.services.GetProjectTitleResponse;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/9 19:24
 */
@GrpcService
@Slf4j
public class AwardMqInitGrpcService extends AwardMqInitServiceGrpc.AwardMqInitServiceImplBase {

    @Autowired
    private AwardAwardDao awardAwardDao;

    @Autowired
    private RedPacketAwardServiceImpl redPacketAwardService;

    @Autowired
    @Qualifier("DrawAwardServiceImpl")
    private DrawAwardServiceImpl drawAwardService;

    @Autowired
    private ProjectGrpcService projectGrpcService;

    @Override
    public void initAwardMq(InitAwardMqRequest request, StreamObserver<InitAwardMqResponse> responseObserver) {
        List<Integer> types = new ArrayList<>();
        types.add(AwardTypeEnum.DRAW.getIndex());
        types.add(AwardTypeEnum.REDPACKET.getIndex());
        List<Integer> statuses = new ArrayList<>();
        statuses.add(AwardStatusEnum.INACTIVE.getIndex());
        statuses.add(AwardStatusEnum.ACTIVE.getIndex());
        statuses.add(AwardStatusEnum.PAUSE.getIndex());
        List<AwardAwardDO> awardAwardDOS = awardAwardDao.listPageAwardAward(null, null, types, statuses, null, null);
        if (!CollectionUtils.isEmpty(awardAwardDOS)) {
            int count = 0;
            for (int i = 0; i < awardAwardDOS.size(); i++) {
                try {
                    AwardAwardDO awardAwardDO = awardAwardDOS.get(i);
                    if (awardAwardDO.getTType().intValue() == AwardTypeEnum.DRAW.getIndex()) {
                        // 未开始发送开始消息
                        if (awardAwardDO.getStatus().intValue() == AwardStatusEnum.INACTIVE.getIndex()) {
                            count++;
                            drawAwardService.sendMqStartMsg(awardAwardDO);
                        }
                        count++;
                        // 发送结束消息
                        drawAwardService.sendMqStopMsg(awardAwardDO);

                    } else if (awardAwardDO.getTType().intValue() == AwardTypeEnum.REDPACKET.getIndex()) {
                        // 未开始发送开始消息
                        if (awardAwardDO.getStatus().intValue() == AwardStatusEnum.INACTIVE.getIndex()) {
                            count++;
                            redPacketAwardService.sendMqStartMsg(awardAwardDO);
                        }
                        count++;
                        // 发送结束消息
                        redPacketAwardService.sendMqStopMsg(awardAwardDO, getProjectTitle(awardAwardDO.getProjectId()));
                    }
                } catch (Exception e) {
                    log.error("[award] 初始化mq消息失败", e);
                }

            }


            log.info(String.format("共生成mq消息%d条", count));
        } else {
            log.info("没有mq延迟消息待生成。");
        }
        responseObserver.onNext(InitAwardMqResponse.newBuilder().setStatus(RpcStatusResult.success()).build());
        responseObserver.onCompleted();
    }

    private String getProjectTitle(String projectId) {
        String  title = "mq消息初始化-默认title";
        try {
            // 获取project.title
            GetProjectTitleRequest.Builder builder = GetProjectTitleRequest.newBuilder();
            builder.setId(projectId);
            GetProjectTitleResponse projectTitleResponse = projectGrpcService.getProjectTitle(builder.build());
            if (projectTitleResponse != null && projectTitleResponse.getStatus().getCodeValue() == Code.OK_VALUE) {
                title = projectTitleResponse.getTitle();
            }
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("查找项目名称失败," + projectId, e);
            }
        }

        return title;
    }
}
